package team.seekers.skill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.skill.entity.dao.*;
import team.seekers.skill.entity.vo.admin.update.AdminBlogUpdateVO;
import team.seekers.skill.entity.vo.admin.update.AdminSectionUpdateVO;
import team.seekers.skill.entity.vo.admin.update.AdminVideoUpdateVO;
import team.seekers.skill.entity.vo.fore.list.SectionListVO;
import team.seekers.skill.entity.vo.fore.detail.SectionDetailVO;
import team.seekers.skill.entity.vo.fore.list.BlogListVO;
import team.seekers.skill.entity.vo.fore.list.VideoListVO;
import team.seekers.skill.mapper.SectionMapper;
import team.seekers.skill.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import team.seekers.skill.common.utils.ConstantUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbc
 * @since 2021-01-18
 */
@Service
public class SectionServiceImpl extends ServiceImpl<SectionMapper, Section> implements SectionService {

    @Autowired
    private UserSectionService userSectionService;
    @Autowired
    private NoteService noteService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private SkillService skillService;
    @Autowired
    private SectionBlogService sectionBlogService;
    @Autowired
    private SectionVideoService sectionVideoService;

    private boolean result;

    @Override
    public List<SectionListVO> listSectionVOByChapterId(String chapterId, String userId) {
        QueryWrapper<Section> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        wrapper.orderByAsc("sequence");
        List<Section> sectionList = list(wrapper);
        List<SectionListVO> sectionListVoList = new ArrayList<>();
        for (Section section : sectionList) {
            SectionListVO sectionListVo = new SectionListVO();
            BeanUtils.copyProperties(section, sectionListVo);
            Integer status = userSectionService.getStatus(userId, section.getId());
            sectionListVo.setStatus(status);
            sectionListVoList.add(sectionListVo);
        }
        return sectionListVoList;
    }
    @Override
    public List<AdminSectionUpdateVO> listAdminSectionUpdateVOByChapterId(String chapterId) {
        QueryWrapper<Section> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        wrapper.orderByAsc("sequence");
        List<Section> sectionList = list(wrapper);
        List<AdminSectionUpdateVO> adminSectionUpdateVOList = new ArrayList<>();
        for (Section section : sectionList) {
            AdminSectionUpdateVO adminSectionUpdateVO = new AdminSectionUpdateVO();
            BeanUtils.copyProperties(section,adminSectionUpdateVO);
            // 添加博客和视频
            List<AdminBlogUpdateVO> adminBlogUpdateVOList = sectionBlogService.listAdminBlogUpdateVOBySectionId(section.getId());
            List<AdminVideoUpdateVO> adminVideoUpdateVOList = sectionVideoService.listAdminVideoUpdateVOBySectionId(section.getId());

            adminSectionUpdateVO.setAdminBlogUpdateVOList(adminBlogUpdateVOList);
            adminSectionUpdateVO.setAdminVideoUpdateVOList(adminVideoUpdateVOList);
            adminSectionUpdateVOList.add(adminSectionUpdateVO);
        }
        return adminSectionUpdateVOList;
    }

    // 开始学习某一节
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean startLearn(String sectionId,String userId) {
        // 判断是不是已经学过了
        if(userSectionService.getStatus(userId,sectionId) != 0){
            return false;
        }
        // 判断是不是第一次学这一章,是则开始学习这一章
        Section section = getById(sectionId);
        QueryWrapper<UserSection> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",section.getChapterId());
        wrapper.eq("user_id",userId);
        int count = userSectionService.count(wrapper);
        if(count == 0){
            boolean startLearn = chapterService.startLearn(section.getChapterId(), userId);
            if(!startLearn){
                return false;
            }
        }
        //创建笔记
        Note note = new Note();
        note.setUserId(userId);
        note.setTitle(section.getTitle());
        note.setContent("请在这里输入.....");
        note.setSectionId(sectionId);
        note.setSkillId(section.getSkillId());
        boolean save1 = noteService.save(note);
        if (!save1){
            return false;
        }
        UserSection userSection = new UserSection();
        userSection.setStatus(ConstantUtils.START_LEARN);
        userSection.setSectionId(sectionId);
        userSection.setUserId(userId);
        userSection.setChapterId(section.getChapterId());
        boolean save = userSectionService.save(userSection);
        return save;
    }

    // 学完某一节
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean endLearn(String sectionId, String userId) {
        // 判断是不是最后一节，是则学完这一章
        QueryWrapper<UserSection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("status",ConstantUtils.END_LEARN);
        Section section = getById(sectionId);
        wrapper.eq("chapter_id",section.getChapterId());
        int count = userSectionService.count(wrapper);
        Chapter chapter = chapterService.getById(section.getChapterId());
        if(count == chapter.getSections()){
            boolean endLearn = chapterService.endLearn(chapter.getId(), userId);
            if (!endLearn){
                return false;
            }
        }
        QueryWrapper<UserSection> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id",userId);
        wrapper1.eq("section_id",sectionId);
        UserSection userSection = userSectionService.getOne(wrapper1);
        userSection.setStatus(ConstantUtils.END_LEARN);
        boolean update = userSectionService.updateById(userSection);
        if (!update)
            return false;
        return true;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean delete(String sectionId){
        // 删除所有笔记
        result = noteService.removeBySectionId(sectionId);
        if(!result){
            return false;
        }
        // 删除所有问题
        result = questionService.removeBySectionId(sectionId);
        if(!result){
            return false;
        }
        // 删除所有记录
        result = userSectionService.removeBySectionId(sectionId);
        if(!result){
            return false;
        }
        // 删除所有博客
        result = sectionBlogService.removeBySectionId(sectionId);
        if(!result){
            return false;
        }
        // 删除所有视频
        result = sectionVideoService.removeBySectionId(sectionId);
        if(!result){
            return false;
        }
        // 删除节
        result = removeById(sectionId);
        return result;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean removeByChapterId(String chapterId){
        QueryWrapper<Section> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        List<Section> sectionList = list(wrapper);
        for (Section section : sectionList) {
            boolean delete = delete(section.getId());
            if(!delete){
                return false;
            }
        }
        return true;
    }

    @Override
    public SectionDetailVO getSectionDetailVO(String sectionId) {
        Section section = getById(sectionId);
        if(section == null){
            return null;
        }
        SectionDetailVO sectionDetailVO = new SectionDetailVO();
        BeanUtils.copyProperties(section,sectionDetailVO);

        QueryWrapper<SectionBlog> wrapper = new QueryWrapper<>();
        wrapper.eq("section_id",section.getId());
        List<SectionBlog> blogList = sectionBlogService.list(wrapper);
        List<BlogListVO> blogListVOList = new ArrayList<>();
        for (SectionBlog sectionBlog : blogList) {
            BlogListVO blogListVO = new BlogListVO();
            BeanUtils.copyProperties(sectionBlog,blogListVO);
            blogListVOList.add(blogListVO);
        }

        QueryWrapper<SectionVideo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("section_id",section.getId());
        List<SectionVideo> vedioList = sectionVideoService.list(wrapper1);
        List<VideoListVO> videoListVOList = new ArrayList<>();
        for (SectionVideo sectionVideo : vedioList) {
            VideoListVO videoListVO = new VideoListVO();
            BeanUtils.copyProperties(sectionVideo, videoListVO);
            videoListVOList.add(videoListVO);
        }

        sectionDetailVO.setBlogListVOList(blogListVOList);
        sectionDetailVO.setVideoListVOList(videoListVOList);
        return sectionDetailVO;
    }


}
